<!DoCtYpE hTmL>
<html manifest="manifest.cache">
<head>
<title>Chrowety</title>
<link rel="icon" href="icones/x16.png" sizes="16x16"/>
<link rel="icon" href="icones/x32.png" sizes="32x32"/>
<link rel="icon" href="icones/x48.png" sizes="48x48"/>
<script>
const Controller = function(){return chrome.extension.getBackgroundPage()}
var gid = function(i){return document.getElementById(i)}
var total = 0,aid,last_id,Interval,isUp = false,LoginUp,Iniciado;
var db = Controller().db;
var Slogan = Traduz('slogan');
var AtivaLog = true;
var Trava = false;
var isDestacado = false;
var bActive = false;
var isFirstPopupOpen = function(){return Controller().isFirstPopupOpen}

window.Interface = {};
Interface.User = {};
Interface.User.Follow = function(uname) // Interface.User.Follow()
{
    var _Container = $('#user_options_follow'),
        _Status = $('#user_options_status');
        _Status.html('<span class="spinner"></span>');
        _Status.show();

    uname = $('#user_options_div').attr('user');
    _Container.addClass('active');
    Controller().Utils.User.Follow(uname, function(re){
        _Status.html(Controller().Utils.Regex.Replace(re));
        _Status.fadeIn();
        _Container.removeClass('active');
    });
}
Interface.User.UnFollow = function(uname) // Interface.User.UnFollow()
{
    var _Container = $('#user_options_unfollow'),
        _Status = $('#user_options_status');
        _Status.html('<span class="spinner"></span>');
        _Status.show();

    uname = $('#user_options_div').attr('user');
    _Container.addClass('active');
    Controller().Utils.User.UnFollow(uname, function(re){
        _Status.html(Controller().Utils.Regex.Replace(re));
        _Status.fadeIn();
        _Container.removeClass('active');
    });
}
Interface.Tweet = {};
Interface.Tweet.getConversation = function(tid, rid, timeline) // Interface.Tweet.getConversation()
{
    var _Container = $('#twets_'+timeline+' #conversations_'+tid),
        _Visible   = _Container.css('display') == 'block' ? true : false,
        _Text = $('#twets_'+timeline+' #in_reply_to_'+tid),
        _TxtBak = _Text.attr('title');

    if(_Visible)
    {
        _Container.slideUp('fast');
        return;
    }

    if(!rid) return;

    _Text.text(Traduz('loading'));

    Controller().Utils.Tweet.Conversation.init(rid, timeline, function(html, erro)
    {

        if(erro)
        {
            //_Text.text(Traduz('error'));
            _Text.text(_TxtBak);
            return;
        }

        try{
            ReplyCancel(tid, timeline);
        }
        catch(e){}
        _Container.empty();
        var hide_link = "<br /><a onclick='Interface.Tweet.getConversation("+tid+", null, \""+timeline+"\")' class='hide_link'>"+Traduz('hide')+"</a>";
        _Container.html(html+hide_link);
        _Container.slideDown('fast');
        Utils.ConversationsSets();
        _Text.text(_TxtBak);
    });
}

function Active(e)
{
    if(bActive[0])
    {
        if(bActive[0].id == 'blists')
        {
            SelList(bActive, true);
        }
        bActive.attr('active', 'false');
    }
    e.attr('active', 'true');
    bActive = e;
}

function Scroll(event)
{
    var _s;
    if(db.pega('AbaAtiva') == 'list')
    {
        _s = $('#twets_list')[0].scrollTop;
        Controller().tws.list[event.data.lista].scroll = _s;
    }
    else
    {
        _s = $('#twets_'+event.data.lista)[0].scrollTop;
        Controller().Chrowety[event.data.lista].scroll = _s;
    }

    if(_s > 5)
    {
        $('.twets').css('border-top', '1px solid #dddddd');
    }else
    {
        $('.twets').css('border-top', 'none');
    }
}

var LT = {
    init:function()
    {
        this.CheckLoadeds();
        this.Auto();
        this.initLists();
    },

    Auto:function()
    {
        if(db.pega('AbaAtiva'))
        {
		  this.Unreads();
          if(db.pega('AbaAtiva') == 'list' && Controller().tws.list[db.pega('ListAtiva')])
          {
			if(!Controller().tws.list[db.pega('ListAtiva')].ativo)
            {
				this.home();
			}
            else
            {
				this.list(db.pega('ListAtiva'));
                //LT.home();
			}
          }else
          {
            this[db.pega('AbaAtiva')]();
          }
        }else
        {
          this.home();
        }
    },

	ClearUnreads:function(name)
    {
		$('#b'+name).attr('count', Controller().tws.mentions.new_count);
		$('#b'+name).blur();
	},
	
	Unreads:function()
    {
		 var tws = Controller().tws;
		 $('#bhome').attr('count', tws.home.new_count).tipsy();
		 $('#bmentions').attr('count', tws.mentions.new_count).tipsy();
		 $('#bmessages').attr('count', tws.messages.new_count).tipsy();
		 $('#bhome').hover();
		 $('#bmentions').hover();
		 $('#bmessages').hover();
	},

    more:function(name, hide, show)
    {
        var b = $('#b'+name);
        if(b.attr('disabled')) return false;
        //Button.ativa(b);
        if(b.attr('ativo') && !show || hide && !show)
        {
            //$('.twets').css('height', $('.twets').css('height')+10);
            $('#submenu_'+name).slideUp('fast');
            b.removeAttr('ativo');
        }else
        {
            //$('.twets').css('height', $('.twets').css('height')-10);
            $('#submenu_'+name).slideDown('fast');
            b.attr('ativo', 'true');
        }
    },

    initLists:function()
    {
        var _tws = Controller().tws;
        if(_tws.lists && _tws.lists.names)
        {
            $('#submenu_lists').empty();
            nomes = _tws.lists.names;
			var button = null, new_count=0;
            var i = nomes.length;
            //for(i=nomes.length-1; i >= 0; i--)
            while(i--)
            {
                if(_tws.list && _tws.list[nomes[i]] && _tws.list[nomes[i]].ativo)
                {
                	button = document.createElement('button');
                    button.id = 'b'+nomes[i];
                    button.innerText = nomes[i].capitalize();
                    button.setAttribute('list', nomes[i]);
                    button.onclick = function(){
                        LT.list(this.getAttribute('list'));
                    }
					$('#submenu_lists').append(button);
					new_count = _tws.list[nomes[i]].new_count ? _tws.list[nomes[i]].new_count : 0;
					$('#b'+nomes[i]).attr('count', new_count).tipsy();
                    //setTimeout(function(){
    					$('#b'+nomes[i]).hover();
                    //}, 200);
                 }
                 else
                 {
                    continue;
                 }
            }
            LT.more('lists', null, true);
            LT.Auto();
        }
    },

    loadList:function(name)
    {
        var _tws = Controller().tws;
        if(_tws.list[name] && _tws.list[name].ativo)
        {
			$('#lists_'+name).removeClass('ds');
			$('#lists_'+name).attr('title', Traduz('add_list'));
            _tws.list[name].ativo = false;
            Controller().SalvaListas();
            Controller().window.clearInterval(Controller().time.list[name]);
        }
        else{
            if(_tws.list[name] && _tws.list[name]._loaded)
            {
				$('#lists_'+name).addClass('ds');
				$('#lists_'+name).attr('title', Traduz('remove_list'));
                Controller().tws.list[name].ativo = true;
                if(!Controller().time.list[name])
                {
                    Controller().time.list[name] = Controller().setInterval('Chrowety.List.Since(\''+name+'\')', Controller().m(Controller().config.refresh['list']));
                }
            }
            else
            {
                $('#show_lists_load_'+name).show();
                $('#lists_'+name)[0].setAttribute('disabled', 'disabled');
				$('#lists_'+name).attr('title', Traduz('add_list'));
                Controller().Chrowety.List.Run(name, 'LT.addList(\''+name+'\')');
            }
        }
        this.initLists();
    },

    addList:function(name)
    {
        $('#show_lists_load_'+name).hide();
        $('#lists_'+name).addClass('ds');
		$('#lists_'+name).attr('title', Traduz('remove_list'));
        $('#lists_'+name)[0].removeAttribute('disabled');		
        this.initLists();
    },

    Lists:function(force)
    {
        $('#user_info').hide();

        if(force)
        {
            $('#show_lists_lists').empty();
            $('#show_lists_load').show();
            $('#show_lists_lists').hide();
            $('#show_lists').attr('loaded', "false");
            Controller().Chrowety.Lists('LT.Lists()');
            return false;
        }

        var _tws = Controller().tws;

        if(_tws.lists && !_tws.lists.loaded)
        {
            Controller().Chrowety.Lists('LT.Lists()');
            return false;
        }

        if(_tws.lists && _tws.lists.loaded && $('#show_lists').attr('loaded') != "true")
        {
            nomes = _tws.lists.names;
            fnomes = _tws.lists.full_names;
            var i = 0, dn_ok, dn_title,lay = '';
            for(i; i < nomes.length; i++)
            //var i = nomes.length, dn_ok, dn_title,lay = '';
            //while(i--)
            {
                dn_ok     = _tws.list[nomes[i]] && _tws.list[nomes[i]].ativo ? 'ds' : '';
                dn_title  = _tws.list[nomes[i]] && _tws.list[nomes[i]].ativo ? Traduz('remove_list') : Traduz('add_list');
                lay +=
                '<div onclick="LT.loadList(this.getAttribute(\'list\'));" title="'+dn_title+'" id="lists_'+nomes[i]+'" list="'+nomes[i]+'" class="list '+dn_ok+'">'+
                    '<span class="fl">'+fnomes[i]+'</span>'+
                    '<span class="fr dn" id="show_lists_load_'+nomes[i]+'"><img src="img/spinner.gif" width="14" height="14" /></span>'+
                '</div>';
            }

            $('#show_lists_lists').append(lay);

            if(i == 0)
            {
                $('#show_lists_lists').append(Traduz('no_lists_found'));
            }

            $('#show_lists_load').hide();
            $('#show_lists_lists').show();

            $('#show_lists').attr('loaded', "true");
        }

        ShowPopup();
        $('#show_lists').css({
          'overflow': 'hidden',
          'max-height': '300px',
        });
        $('#show_lists_lists').css({
          'overflow': 'scroll'
        });
        $('#show_lists').show();
    },

    CheckLoadeds:function()
    {
        var _ch = Controller().Chrowety;
        var tms = ['home', 'mentions', 'messages', 'favorites'];
        var i = tms.length;
        while(i--)
        {
            if(_ch[tms[i]]._loaded)
            {
                var b = $('#b'+tms[i]);
                b.removeAttr('alerta');
                b.removeAttr('disabled');
            }
        }
    },

    HideLoad:function()
    {
        $('#load').hide();
        $('#load_result').text('');
    },

    list:function(name, force)
    {
        this.loadTimeline(name, true, force);
    },
    home:function(force)
    {
        this.loadTimeline('home', null, force);
    },
    mentions:function(force)
    {
        this.loadTimeline('mentions', null, force);
    },
    messages:function(force)
    {
        this.loadTimeline('messages', null, force);
    },
    favorites:function(force)
    {
        this.loadTimeline('favorites', null, force);
    },

    loadTimeline:function(time, list, force)
    {
        var name = time;
        var b = $('#b'+name);
        b.removeAttr('alerta');
        b.removeAttr('disabled');
        if(b.attr('disabled'))
        {
            return false;
        }
        Button.ativa(b);

        var _ch = !list ? Controller().Chrowety[name] : Controller().tws.list[name];
        var tcon = !list ? ('#tcon_'+name) : '#tcon_list';
        var ttws = !list ? ('#twets_'+name) : '#twets_list';
        var loaded = _ch._loaded;
        if(!loaded)
        {
            _ch.scroll = 0;
            _ch.Run('LT.'+name+'()');

            return false;
        }
        if(force)
        {
            $('#load').show();
            $('#load_result').text('');
            _ch.Since();
            if(time === 'messages')
            {
                _ch.Run(null,true);
            }
            return false;
        }
        db.salva('AbaAtiva', (!list ? name : 'list'));
        if(list)
        {
            db.salva('ListAtiva', name);
        }

        var _tws_ = (!list ? Controller().fec.html(name) : Controller().fec.html(name, null, null, true));

        if(_tws_ != "")
        {
            var tws = _tws_;
            var tsh = $('<div id="showMore_link_'+(list?'list':name)+'" class="show_more" onclick="ShowMore(\''+name+'\' '+(list?',true':'')+')" title="'+Traduz('show_more')+'">'+Traduz('show_more')+'</div>');
        }
        else
        {
            var tws = Traduz('no_more_tweets');
            var tsh = '';
        }
        $('#new_twets_'+name).hide();
        $('#load_result').text('Loading '+name+'...');
        $('.twets_con').hide();
        $(tcon).show();
        if(list) $(tcon).attr('lista', name);
        $(ttws).empty();
        $(ttws).html(tws);
        $(ttws).append(tsh);
        SearchData();
        $(ttws)[0].scrollTop = _ch.scroll;
        $(ttws).bind("scroll", {lista:name}, Scroll);
        Scroll({data:{lista:name}});
        $('#load').hide();
        this.ClearUnreads(name);
		window.setTimeout("Controller().ClearUnreads('"+name+"')", 500);
		Utils.UISets();
    }
};

var Button = {
    ativa:function(el)
    {
        Active($(el));
    }
};

function Traduz(s, e)
{
    return Controller().Traduz(s, e);
}

function html(r)
{
    return document.write(r);
}

function LoadProfile()
{
    $('.info').show();
    if(db.pega('profile_loaded')){
        $('#username').text(db.pega('user'));
        $('#user_nome').text(db.pega('nome'));
        $('#seguindo').html(db.pega('seguindo') + ' ' + Traduz('following'));
        $('#seguidores').html(db.pega('seguidores') + ' ' + Traduz('followers'));
        $('#foto').attr('src', db.pega('foto'));
        $('#PhotoLink').attr('nohref', 'http://twitter.com/home');
        $('#SeguindoLink').attr('nohref', 'http://twitter.com/'+db.pega('user')+'/following');
        $('#SeguidoresLink').attr('nohref', 'http://twitter.com/'+db.pega('user')+'/followers');
        $('#foto_load').hide();
        $('#foto').show();
        $('#Profi_Data').show();
    }
}

function RefreshResult(n, time)
{
    if(n > 0)
    {
        var t = n > 1 ? ' '+Traduz('new_tweets') : ' '+Traduz('new_tweet');
        $('#new_twets_'+time).fadeIn();
        $('#new_twets_'+time).text(n+t);
        LT.Unreads();
    }

    document.title = '('+Controller().MostraCount()+') Chrowety ('+db.pega('user')+')';
}

function RefreshTweets(time, list)
{
    Controller().ClearUnreads(time);
    if(list)
    {
        $('#new_twets_list').fadeOut();
        eval('LT.list('+time+')');
    }
    else
    {
        $('#new_twets_'+time).fadeOut();
        eval('LT.'+time+'()');
    }

    document.title = '('+Controller().MostraCount()+') Chrowety ('+db.pega('user')+')';
}

function RefreshLimit(n){
    if(n)
    {
        if(n == 0)
        {
            $('#load_result').text(Traduz('api_excedeed'));
            $('#load_image').hide();
        }
    }
    else
    {
        if(Controller().api_limit == 0)
        {
            $('#load_result').text(Traduz('api_excedeed'));
            $('#load_image').hide();
        }
        else
        {

        }
    }
    $('#load').fadeIn();
}

function SearchData()
{
    $('.tweetdata').each(function(n,w){
        $(w).text(Controller().TweetData($(w).attr('data')));
    });
}

function ShowMore(timeline, list)
{
    if(list)
    {
        $('#showMore_link_list').html($('<img id="showMore_load" src="img/spinner.gif" width="14" height="14" />'));
        Controller().Chrowety.List.Max(timeline);
    }
    else
    {
        $('#showMore_link_'+timeline).html($('<img id="showMore_load" src="img/spinner.gif" width="14" height="14" />'));
        Controller().Chrowety[timeline].Max();
    }
}

function ShowMoreOK(old, timeline, list)
{
    $('#showMore_link_'+timeline).remove();
    if(old)
    {
        $('#twets_'+timeline).append(old);
        SearchData();
        if(list)
        {
            $('#twets_'+timeline).append($('<div id="showMore_link_list" class="show_more" onclick="ShowMore(\''+list+'\', true)" title="'+Traduz('show_more')+'">'+Traduz('show_more')+'...</div>'));
        }
        else
        {
            $('#twets_'+timeline).append($('<div id="showMore_link_'+timeline+'" class="show_more" onclick="ShowMore(\''+timeline+'\')" title="'+Traduz('show_more')+'">'+Traduz('show_more')+'...</div>'));
        }
        Utils.UISets();

    }
    else
    {
        $('#twets_'+timeline).append($('<div class="no_more">'+Traduz('no_more_tweets')+'</div>'));
    }
}

function ShowMoreFAIL(old, timeline, list)
{
    $('#showMore_link_'+timeline).remove();
    if(list)
    {
        $('#twets_'+timeline).append($('<div id="showMore_link_list" class="show_more" onclick="ShowMore(\''+list+'\', true)" title="'+Traduz('show_more')+'">'+Traduz('show_more')+'...</div>'));
    }
    else
    {
        $('#twets_'+timeline).append($('<div id="showMore_link_'+timeline+'" class="show_more" onclick="ShowMore(\''+timeline+'\')" title="'+Traduz('show_more')+'">'+Traduz('show_more')+'...</div>'));
    }
}

function Link(e, ev, h){
    if(h)
    {
        chrome.tabs.create({url: chrome.extension.getURL('conf.html')});
    }else
    {
        var b = ev.button;
        var href = e.getAttribute('nohref') ? e.getAttribute('nohref') : e.href;
        if(b==0 && Controller().config.link_in_bg !="true")
        {
            chrome.tabs.create({url: href, selected: true});
        }
        else if(b==1 || Controller().config.link_in_bg == "true")
        {
            chrome.tabs.create({url: href, selected: false});
        }
    }
}

function DisableButton(tid, tm, str)
{
    $('#twets_'+tm+' #'+(str?str:'botao_')+tid)[0].setAttribute("disabled","disabled");
}

function Count(tid, time, pulaBak)
{
    var r = $('#twets_'+time+' #reply_text_'+tid);
    if(tid == 'update')
    {
        db.salva('update_backup', r.val());
    }
    else
    {
        if(!pulaBak)
        {
            BackupReply(tid, r.val(), time);
        }
    }
    var n = parseInt(140-r.val().length), el = $('#twets_'+time+' #count_'+tid);
    r.attr('count', n);
    if(n < 0)
    {
        DisableButton(tid, time);
        el.addClass('vermelho');
    }
    else if(n == 140)
    {
        DisableButton(tid, time);
    }
    else
    {
        $('#twets_'+time+' #botao_'+tid).removeAttr("disabled");
        el.removeClass('vermelho');
    }
    el.text(n);
}

function DeletaDM(tid, tm)
{
    Controller().Chrowety.deletaDM(tid, tm);
    $('#twets_'+tm+' #deleta_button_'+tid).hide();
    $('#twets_'+tm+' #deleta_load_'+tid).attr('src', 'img/spinner.gif');
    $('#twets_'+tm+' #deleta_load_'+tid).show();
}

function Deleta(tid, tm, rt)
{
    if(!rt)
    {
        Controller().Chrowety.deleta(tid, tm);
        $('#twets_'+tm+' #deleta_button_'+tid).hide();
        $('#twets_'+tm+' #deleta_load_'+tid).attr('src', 'img/spinner.gif');
        $('#twets_'+tm+' #deleta_load_'+tid).show();
    }else
    {
        Controller().Chrowety.deleta(rt, tm, tid);
        $('#twets_'+tm+' #data_retweet_'+tid).html('<span class="fr">'+Traduz('undoing')+'</span>');
    }
}

function DeletaOK(tid, tm)
{
    $('#twets_'+tm+' #tweply_'+tid).fadeOut(function(){
      $('#twets_'+tm+' #tweply_'+tid).remove();
    });
}

function DeletaFAIL(tid, tm)
{
    $('#twets_'+tm+' #deleta_button_'+tid).show();
    $('#twets_'+tm+' #deleta_load_'+tid).hide();
}

function UndoRTFAIL(rt, tm, tid)
{
    console.log('UndoRTFAIL()');
    $('#twets_'+tm+' #data_retweet_'+tid).html('<a onclick="Deleta('+tid+', \''+tm+'\', '+rt+');" class="opts fr" title="'+Traduz('undo_retweet')+'">'+Traduz('undo_retweet')+'</a>');
}

function UndoRTOK(rt, tm, tid)
{
    console.log('UndoRTOK()');
    if($('#twets_'+tm+' #retweeted_status_by_other_'+tid)[0])
    {
      $('#twets_'+tm+' #retweeted_status_by_me_'+tid).fadeOut(function(){
          $('#twets_'+tm+' #retweeted_status_by_me_'+tid).remove();
      });
    }
    else
    {
      $('#twets_'+tm+' #retweeted_status_'+tid).fadeOut(function(){
          $('#twets_'+tm+' #retweeted_status_'+tid).remove();
      });
    }
    $('#twets_'+tm+' #data_retweet_'+tid).html('<a onclick="Retweet('+tid+', \''+tm+'\');" class="opts fr" title="'+Traduz('retweet')+'">'+Traduz('retweet')+'</a>');
}

function Favorite(tid, tm)
{
    var list = null;
    if(tm == 'list')
    {
        list = $('#tcon_list').attr('lista');
    }
    Controller().Chrowety.favorite(tid, tm, list);
    $('#twets_'+tm+' #tweet_fav_'+tid).removeClass("opts");
    $('#twets_'+tm+' #tweet_fav_'+tid).addClass("dn");
    $('#twets_'+tm+' #tweet_ifav_'+tid).attr('src', 'img/spinner.gif');
    $('#twets_'+tm+' #tweet_ifav_'+tid).show();
}

function UnFavorite(tid, tm)
{
    var list;
    if(tm == 'list')
    {
        list = $('#tcon_list').attr('lista');
    }
    Controller().Chrowety.unfavorite(tid, tm, list);
    $('#twets_'+tm+' #tweet_fav_'+tid).addClass("dn");
    $('#twets_'+tm+' #tweet_ifav_'+tid).attr('src', 'img/spinner.gif');
    $('#twets_'+tm+' #tweet_ifav_'+tid).show();

}

function FavOK(tid, fav, tm)
{
    var Star = $('#twets_'+tm+' #tweet_fav_'+tid);
    $('#twets_'+tm+' #tweet_ifav_'+tid).hide();
    if(fav)
    {
        $('#twets_'+tm+' #tweet_fav_'+tid).removeClass("dn");
        Star.attr({
            title: Traduz('unfavorite'),
            fav:"true",
        });
        Star.unbind();
        Star.bind("click", function(){return UnFavorite(tid, tm)});
    }
    else
    {
        Star.addClass("opts");
        Star.attr("title", Traduz('favorite'));
        Star.removeAttr('fav');
        Star.unbind();
        Star.bind("click", function(){return Favorite(tid, tm)});
        if(tm == 'favorites')
        {
            $('#twets_'+tm+' #tweply_'+tid).fadeOut("slow", function(){
                $('#twets_'+tm+' #tweply_'+tid).remove();
            });
        }
    }
}

var AtalhoDisabled = false;
function Atalho(evt, tid, tm)
{
    var key = evt.keyCode;
    if(evt.ctrlKey)// Ctrl
    {
        switch(key)
        {
            case 8013: // Ctrl + Enter
                    if(!tid && !tm)
                    {
                        //Update();
                    }
                    evt.returnValue=false;
                    break;
            case 65: // A
                    if(tid == 'update')
                    {
                        //$('#botao_update_cancel').click();
                    }
                    else
                    {
                        //ReplyCancel(tid, tm);
                        //$('#botao_'+tid+'_cancel').click();
                    }
                    //evt.returnValue=false;
                    break;
        }
    }
    else
    {
        switch(key)
        {
            case 13: // Enter
                if($('#twets_'+tm+' #botao_'+tid).attr('disabled') != "disabled" || !AtalhoDisabled)
                {
                    if(tid == 'update')
                    {
                        if(!$("#reply_text_update").val().length)
                        {
                            evt.returnValue=false;
                        }
                        else
                        {
                            $('#botao_update').click();
                        }
                    }
                    else
                    {
                        if($('#twets_'+tm+' #reply_text_'+tid)[0] && $('#twets_'+tm+' #reply_text_'+tid)[0].value)
                        {
                            if(tm == 'messages')
                            {
                                $('#twets_'+tm+' #botao_'+tid).click();
                                AtalhoDisabled = true;
                            }
                            else
                            {
                                $('#twets_'+tm+' #botao_'+tid).click();
                                AtalhoDisabled = true;
                            }
                        }
                        else
                        {
                            evt.returnValue=false;
                        }
                    }
                    evt.returnValue=false;
                }
                break;
        }
    }
}

function doUpdate()
{
    DisableButton('update', 'update');
    var txt = $('#reply_text_update').val();
    console.log('Sending update: '+txt);
    $('#reply_status_update').text(Traduz('sending')+'...');
    Controller().Chrowety.update(txt, null, 'home');
    DisableButton('update', 'update');
}

function Update()
{
    if(isUp == true)
    {
        return false;
    }

    $('#reply_text_update').animate({height: "50px",opacity: 0.5}, "fast", function(){
        isUp = true;
        var bak = db.pega('update_backup');

        if(bak && bak != Slogan)
        {
            $("#reply_text_update").val(db.pega('update_backup'));
        }
        else
        {
            $("#reply_text_update").val('');
            $("#botao_share_update").removeAttr('disabled');
        }
        var height = parseInt(Controller().config.popup_height.split('px')[0]);
        Count('update', 'update');
    });
    $("#update_co2").slideDown("fast", function(){
        Utils.Size.Adjust();
    });
    $("#reply_text_update").focus();
}

function UpdateCancel()
{
    $('#reply_text_update').animate({height: "20px",opacity: 0.3}, "fast", function(){
        //LT.Auto();
        Count('update', 'update');
        $('#twets_update #reply_status_update').text('');
        $("#twets_update #reply_text_update").val(Slogan);
        db.deleta('update_backup');
        isUp = false;
        HideShortLink('update', 'update');
    });
    $('#update_co2').slideUp("fast", function(){
        Utils.Size.Adjust();
    });
}

function UpdateOK(re, tm, DM)
{
    if(!DM)
    {
        $('#reply_status_update').text(Traduz('ok'));
        UpdateCancel();
        HideShortLink('update', 'update');
    }
    $('#twets_'+tm).html(re+$('#twets_'+tm).html());
    SearchData();
    Utils.UISets();
}

function UpdateFAIL(tid, tm, status)
{
    $('#reply_status_update').html('<span style="color:red">'+Traduz(status)+'.</span>');
}

function ReplyDMFAIL(tid, time, status)//Esconde o elemento de Reply
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).html('<span style="color:red">'+Traduz(status)+'.</span>');
}

function ReplyDMOK(tid, time, ht)//Esconde o elemento de Reply
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('ok'));
    $('#twets_'+time+' #reply_'+tid).slideUp(function()
    {
        $('#twets_'+time+' #reply_text_'+tid).val('');
        Count(tid, time);
        HideShortLink(tid);
        $('#twets_'+time+' #reply_status_'+tid).text('');
        UpdateOK(ht, time, true);
    });
}

function ReplyDM(tid, time, other)
{
    var bak  = $('#twets_'+time+' #reply_text_'+tid).attr('backup');
    var txt  = bak ? bak : '';
    $('#twets_'+time+' #reply_text_'+tid).removeAttr('disabled');
    $('#twets_'+time+' #retweet_choice_'+tid).hide();
    $('#twets_'+time+' #reply_rt_'+tid).show();
    $('#twets_'+time+' #botao_'+tid).removeAttr('onclick');


    $('#twets_'+time+' #botao_'+tid)[0].onclick = function(){
          doReplyDM(tid, time, (other ? true : false));
    }
    $('#twets_'+time+' #reply_'+tid).slideDown("fast", function(){
       $('#twets_'+time+' #reply_rt_'+tid).show();
       $('#twets_'+time+' #reply_text_'+tid).focus();
       $('#twets_'+time+' #reply_text_'+tid).val(txt);
       Count(tid, time, true);
       //console.log($('#twets_'+time+' #botao_'+tid)[0]);
    });
}

function doReplyDM(tid, time, other)
{
    DisableButton(tid, time);
    var user = $('#twets_'+time+' #user_'+tid).val();
    var txt = $('#twets_'+time+' #reply_text_'+tid).val();
    Controller().console.log('Sending Message: '+txt+' to user: '+user);
    $('#twets_'+time+' #new_retweet_'+tid).hide();
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('sending')+'...');
    Controller().Chrowety.updateDM(txt, user, tid, (other ? time : null));
    BackupReplyCancel(tid, time);
}

function doReply(tid, time, rt)
{
    DisableButton(tid, time);
    var txt = $('#twets_'+time+' #reply_text_'+tid).val();
    console.log('Sending: '+txt+' tid: '+tid);
    $('#twets_'+time+' #new_retweet_'+tid).hide();
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('sending')+'...');
    Controller().Chrowety.update(txt, tid, time, null, rt);
    BackupReplyCancel(tid, time);
}

//> Inicio Short Link
function HideShortLink(tid, time)
{
    $('#twets_'+time+' #short_link_url_'+tid).animate({
            width: "1.75em",
            opacity: 0.5
    },500);
    $('#twets_'+time+' #short_link_url_'+tid).val('');
}

function ShowShortLink(tid, time)
{
    Controller().Utils.GetTab(function(tab)
    {
        $('#twets_'+time+' #short_link_url_'+tid).val(tab.url);
        $('#twets_'+time+' #short_link_url_'+tid).select();
    });

    $('#twets_'+time+' #short_link_url_'+tid).animate({
            width: (tid == 'update' ? "300px" : "210px"),
            opacity: 1.0
    },500);
}

function ShortLink(tid, time)
{
    var url = $('#twets_'+time+' #short_link_url_'+tid).val();
    if(!url)
    {
        ShowShortLink(tid, time);
        return false;
    }
    $('#twets_'+time+' #short_link_url_'+tid).val(Traduz('shorting_url')+'...');
    Controller().Utils.Short.short(url, tid, time);
}

function ShortLinkOK(tid, time, url)
{
    $('#twets_'+time+' #short_link_url_'+tid).val(Traduz('ok')+'!');
    $('#twets_'+time+' #reply_text_'+tid).focus();
    $('#twets_'+time+' #reply_text_'+tid).val($('#twets_'+time+' #reply_text_'+tid).val()+''+url+' ');
    Count(tid, time);
    HideShortLink(tid, time);
}
//> FIM Short Link


//> Share Page
function SharePage(tid, time)
{
    Controller().Utils.GetTab(function(tab)
    {
        Controller().Utils.Short.short(tab.url, tid, time, 'SharePageOK');
        Controller().Utils.Short.title = tab.title;
        $('#twets_'+time+' #reply_status_'+tid).text(Traduz('wait'));
    });
}

function SharePageOK(tid, time, url)
{
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('ok')+'...');
    var title = Controller().Utils.Short.title;
    var url   = Controller().Utils.Short.link;
    var txt   = title+': '+url;
    $('#twets_'+time+' #reply_text_'+tid).focus();
    $('#twets_'+time+' #reply_text_'+tid).val(txt);
    Count(tid, time);
    DisableButton(tid, time, 'botao_share_');
    HideShortLink(tid, time);
    $('#twets_'+time+' #reply_status_'+tid).text('');
}


function BackupReply(tid, txt, time)
{
    $('#twets_'+time+' #reply_text_'+tid).attr('backup', txt);
    if(time == 'list')
    {
        var lista = $('#tcon_list').attr('lista');
        Controller().fec.ByTid(time, tid, null, lista).reply_backup = txt;
    }
    else
    {
        Controller().fec.ByTid(time, tid).reply_backup = txt;
    }
}

function BackupReplyCancel(tid, time)
{
    $('#twets_'+time+' #reply_text_'+tid).removeAttr('backup');
    if(time == 'list')
    {
        var lista = $('#tcon_list').attr('lista');
        Controller().fec.ByTid(time, tid, null, lista).reply_backup = false;
    }
    else
    {
        Controller().fec.ByTid(time, tid).reply_backup = false;
    }
}

function Reply(tid, status, time, rt)//Mostra o elemento de Reply
{
    var user = '@'+$('#twets_'+time+' #user_'+tid).val();
    var bak  = $('#twets_'+time+' #reply_text_'+tid).attr('backup');
    var txt  = bak && !status ? bak : (status ? status : user+' ');
    $('#twets_'+time+' #retweet_choice_'+tid).hide();
    $('#twets_'+time+' #reply_rt_'+tid).show();
    $('#twets_'+time+' #botao_'+tid)[0].onclick = function()
    {
          doReply(tid, time, rt);
    }

    try
    {
        Interface.Tweet.getConversation(tid, null, time);
    }
    catch(e){}
    $('#twets_'+time+' #reply_'+tid).slideDown("fast", function(){
       if(status)
       {
         $('#twets_'+time+' #reply_text_'+tid).attr('disabled', 'disabled');
         $('#twets_'+time+' #reply_rt_'+tid).hide();
         $('#twets_'+time+' #retweet_choice_'+tid).show();
       }
       else
       {
         $('#twets_'+time+' #reply_rt_'+tid).show();
         $('#twets_'+time+' #reply_text_'+tid).removeAttr('disabled');
         $('#twets_'+time+' #retweet_choice_'+tid).hide();
       }

       $('#twets_'+time+' #reply_text_'+tid).focus();
       $('#twets_'+time+' #reply_text_'+tid).val(txt);
       Count(tid, time, true);
    });
}

function ReplyCancel(tid, time)//Esconde o elemento de Reply
{
    $('#twets_'+time+' #reply_status_'+tid).text('');
    $('#twets_'+time+' #reply_'+tid).slideUp("fast", function()
    {
        BackupReplyCancel(tid, time);
        $('#twets_'+time+' #retweet_choice_'+tid).hide();
        $('#twets_'+time+' #reply_text_'+tid).removeAttr('disabled');
        $('#twets_'+time+' #reply_text_'+tid).val('');
        Count(tid, time);
        HideShortLink(tid);
    });
}

function ReplyOK(tid, time)//Esconde o elemento de Reply
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('ok')+'...');
    $('#twets_'+time+' #reply_'+tid).slideUp(function()
    {
        $('#twets_'+time+' #reply_text_'+tid).val('');
        init();
        Count(tid, time);
        HideShortLink(tid);
    });
}

function ReplyFAIL(tid, time, status)//Esconde o elemento de Reply
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).html('<span style="color:red">'+Traduz(status)+'.</span>');
}

function RetweetOficial(tid, time)//Mostra o elemento do Reply com o Retweet
{
    DisableButton(tid, time, 'rt_now_');
    console.log('Retweeting... tid: '+tid);
    $('#twets_'+time+' #edit_rt_'+tid).hide();
    $('#twets_'+time+' #rt_now_'+tid).text(Traduz('wait')+'...');
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('retweeting')+'...');
    Controller().Chrowety.retweet(tid, time);
}

function RetweetEdit(tid, time)
{
    $('#twets_'+time+' #reply_text_'+tid).removeAttr('disabled');
    $('#twets_'+time+' #reply_status_'+tid).text('');
    $('#twets_'+time+' #retweet_choice_'+tid).hide();
    $('#twets_'+time+' #reply_rt_'+tid).show();
    var a = $('#twets_'+time+' #reply_text_'+tid).val();
    $('#twets_'+time+' #reply_text_'+tid).val('');
    $('#twets_'+time+' #reply_text_'+tid).focus();
    $('#twets_'+time+' #reply_text_'+tid).val(a);
}

function Retweet(tid, time)//Mostra o elemento do Reply com o Retweet
{
    var user = '@'+$('#twets_'+time+' #user_'+tid).val(),
    txt = 'RT '+user+': '+$('#twets_'+time+' #texto_'+tid).text();
    Reply(tid, txt, time, true);
}

function RetweetRT(tid, time)//Mostra o elemento do Reply com o Retweet
{
    var txt = unescape($('#twets_'+time+' #texto_'+tid).attr("data"));
    Reply(tid, txt, time);
}

function RetweetOK(tid, time, rt_id)
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).text(Traduz('ok')+'...');
    $('#twets_'+time+' #reply_'+tid).slideUp(function()
    {
        $('#twets_'+time+' #reply_status_'+tid).text('');
        $('#twets_'+time+' #reply_text_'+tid).val('');
        $('#twets_'+time+' #edit_rt_'+tid).show();
        $('#twets_'+time+' #rt_now_'+tid).removeAttr('disabled');
        $('#twets_'+time+' #rt_now_'+tid).text(Traduz('retweet_now'));
        var rt_user = db.pega('user');
        if($('#twets_'+time+' #retweeted_status_by_other_'+tid)[0])
        {
          $('#twets_'+time+' #retweeted_status_'+tid).append('<span id="retweeted_status_by_me_'+tid+'">, <a class="reter p8" nohref="http://twitter.com/'+rt_user+'" onclick="ShowUser(\''+rt_user+'\')" title="'+rt_user+'">'+Traduz('you')+'</a></span>');
        }
        else
        {
          $('#twets_'+time+' #data_rt_'+tid).append('<span id="retweeted_status_'+tid+'"><br /><span class="cinza p8" title="'+Traduz('retweeted_by')+' '+rt_user+'">'+Traduz('retweeted_by')+' </span><a class="reter p8" nohref="http://twitter.com/'+rt_user+'" onclick="ShowUser(\''+rt_user+'\')" title="'+rt_user+'">'+Traduz('you')+'</a></span>');
        }
        $('#twets_'+time+' #data_retweet_'+tid).html('<a onclick="Deleta('+tid+', \''+time+'\', '+rt_id+');" class="opts fr" title="'+Traduz('undo_retweet')+'">'+Traduz('undo_retweet')+'</a>');
    });
}

function RetweetFAIL(tid, time, status)
{
    AtalhoDisabled = false;
    $('#twets_'+time+' #reply_status_'+tid).html('<span style="color:red">'+Traduz(status)+'.</span>');
    $('#twets_'+time+' #edit_rt_'+tid).show();
    $('#twets_'+time+' #rt_now_'+tid).removeAttr('disabled');
    $('#twets_'+time+' #rt_now_'+tid).text(Traduz('retweet_now'));
}

function LoginStatus(t)
{
    var re = $('#status_login');
    re.text(t);
}

function GoLogin()
{
    $('#init').fadeOut(function()
    {
        $('#OAuthLogin').fadeIn();
        window.close();
    });
}

function PictureZoom(el, outforce)
{
    var zoom = $(el).attr('zoom');
    if(zoom == 'in' && !outforce)
    {
        //var newsrc = $(el).attr('src').replace('_bigger', '');
        var newsrc = $(el).attr('src').replace('_b', '_o');
        var newImage = new Image();
        newImage.src = newsrc;
        $('#user_data').hide();
        $('#user_rest').hide();
        $(el).animate({height: "300px", width: "300px"}, 600,function()
        {
            $(el).attr('zoom', 'out');
            $(el).attr('src', newsrc);
        });
    }
    else
    {
        $('#user_data').show();
        $('#user_rest').show();
        $(el).animate({height: "100px", width: "100px"}, 600,function()
        {
            $(el).attr('zoom', 'in');
        });
    }
}

function ShowUser(uname)
{
    $('.popup_bg').fadeIn();
    Controller().Chrowety.profile_last_view = uname;
    Controller().Chrowety.user(uname);
}

function ShowUserERRO(func)
{
    Controller().Utils.Twitpic.vendo = null;
    Controller().Utils.Yfrog.vendo = null;
    Controller().Utils.Twitgoo.vendo = null;
    Controller().Chrowety.profile_last_view = null;
    $('#user_protegido').addClass('dn');
    $('#user_info').hide();
    $('#map_preview').hide();
    $('#user_unome').empty();
    $('#user_uname').empty();
    $('#user_uname').attr('href', '');
    $('#user_local').empty();
    $('#user_bio').empty();
    $('#user_link').attr('href', '');
    $('#user_link').empty();
    $('#user_photo').attr('src', '');
    $('#user_followers').empty();
    $('#user_following').empty();
    $('#user_tweets').empty();
    $('#user_update').empty();
    $('#user_update_data').empty();
    $('#user_update_data').attr('href', '');
    $('#user_update_via').empty();
    $('#following_re').empty();
    $('#user_options_div').hide();
    $('#user_options_status').text('').hide();

    $('#user_options_follow')[0].removeEventListener('click', Interface.User.Follow);
    $('#user_options_unfollow')[0].removeEventListener('click', Interface.User.UnFollow);

    PictureZoom($('#user_photo'), true);
    $('.popup').css('min-height', '200px');
    HidePopup();
    if(func)
    {
        func();
    }
}

function HidePopup()
{
    $('.popup').hide();
    $('#youtube_preview').hide();
    $('#twitpic_preview').hide();
    $('#user_info').hide();
    $('#quick_profile').hide();
    $('#add_photo').hide();
    $('#show_lists').hide();
    $('.popup_bg').hide();
    $('#special_chars').hide();
}

function ShowPopup()
{
    $('.popup_bg').show();
    $('.popup').fadeIn("fast");
    var lef = ( ($('#init').css('width').split('px')[0]) - ($('.popup').css('width').split('px')[0]) ) / 2;
    var top = ( ($('#init').css('height').split('px')[0]) - ($('.popup').css('height').split('px')[0])) / 2;
    $('.popup').css({
        'margin-left':lef+'px',
        'margin-top':top+'px',
    });
}

function ShowUserOK(user)
{
    var u = user;
    var _u = Controller().Utils;
    $('#user_unome').text(u.name);
    $('#user_uname').text('@'+u.screen_name);
    $('#user_uname').attr('nohref', 'http://twitter.com/'+u.screen_name);
    $('#user_local').text(u.location);
    $('#user_local_link').attr('title', u.location);
    $('#user_local_link').attr('nohref', 'http://maps.google.com/?z=17&q='+u.location);
    $('#user_bio').html(_u.Regex.Replace(u.description));
    $('#user_link').attr('nohref', u.url);
    $('#user_link').text(u.url);
    //$('#user_photo').attr('src', u.profile_image_url.replace('_normal', '_bigger'));
        var img = 'http://img.tweetimag.es/i/'+u.screen_name+'_b';
        $('#user_photo').attr('src', img);
    $('#user_followers').text(_u.FormataNumero(u.followers_count));
    $('#user_following').text(_u.FormataNumero(u.friends_count));
    $('#user_tweets').text(Controller().Utils.FormataNumero(u.statuses_count));
    $('#user_since').text(Controller().SinceDate(u.created_at));

    if(u.verified)
    {
        $('#user_verified').show();
    }
    else
    {
        $('#user_verified').hide();
    }

    if(!u.following)
    {
        $('#following_re').empty();
        $('#following_re').html(Traduz('no'));
    }
    else
    {
        $('#following_re').empty();
        $('#following_re').html(Traduz('yes'));
    }

    if(!u.protected)
    {
        var data = Controller().TweetData(u.status.created_at);
        var source = u.status.source.replace('rel=\"nofollow\"', 'class="cinza p8 source" onclick="Link(this, event);"');
        source = source.replace('href=', 'nohref=');
        var txt = u.status.text;
        txt = _u.Regex.Replace(txt);
        $('#user_update').html(txt);
        $('#user_update_data').text(data);
        $('#user_update_data').attr('nohref', 'http://twitter.com/'+u.screen_name+'/status/'+u.status.id+'');
        $('#user_update_via').html(' via '+source);
        $('#user_update_div').show();
    }
    else
    {
        $('#user_protegido').removeClass('dn');
        $('#user_update_div').hide();
    }

    $('#user_options_follow').attr('title', Traduz('follow')+' @'+u.screen_name);
    $('#user_options_unfollow').attr('title', Traduz('unfollow')+' @'+u.screen_name);

    $('#user_options_follow')[0].addEventListener('click', Interface.User.Follow);
    $('#user_options_unfollow')[0].addEventListener('click', Interface.User.UnFollow);

    $('#user_options_div').attr('user', u.screen_name);
    $('#user_options_div').show();
    $('#user_info').show();
    ShowPopup();
}


function Twitgoo(id, url)
{
    if(!url)
    {
        Controller().Utils.Twitgoo.thumb(id);
        $('#user_info').hide();
        ShowPopup();
    }
    else
    {
        $('#twitpic_link').attr('nohref', 'http://twitgoo.com/'+id);
        $('#twitpic_photo').attr('src', url);
        $('#twitpic_preview').show();
    }
}

function Yfrog(id)
{
    var url = Controller().Utils.Yfrog.thumb(id);
    $('#twitpic_link').attr('nohref', 'http://yfrog.com/'+id);
    $('#twitpic_photo').attr('src', url);
    $('#twitpic_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Twitpic(id)
{
    var url = Controller().Utils.Twitpic.thumb(id);
    $('#twitpic_link').attr('nohref', 'http://twitpic.com/'+id);
    $('#twitpic_photo').attr('src', url);
    $('#twitpic_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Tweetphoto(id)
{
    var url = Controller().Utils.Tweetphoto.thumb(id);
    $('#twitpic_link').attr('nohref', id);
    $('#twitpic_photo').attr('src', url);
    $('#twitpic_preview').show();
    $('#user_info').hide();
    $('.popup').css('min-height', '200px');
    ShowPopup();
}

function AddPhoto(tid, tm)
{
    $('#user_info').hide();
    $('#add_photo').show();
    ShowPopup();
}

function QuickProfile()
{
    $('#user_info').hide();
    $('#quick_profile').css('vertical-align', 'middle');
    $('.popup').css('min-height', '10px');
    ShowPopup();
    $('#quick_profile').show();
    $('#quick_profile_name').focus();
    $('#quick_profile_name').select();
}

function doQuickProfile()
{
    if($('#quick_profile_name').val())
    {
        ShowUserERRO(function()
        {
            ShowUser($('#quick_profile_name').val());
        });
    }
    else
    {
        $('#quick_profile_name').focus();
    }

    return false;
}

function Youtube(id)
{
    var url  = "http://www.youtube.com/v/"+id+'&feature=player_embedded&fs=1';
    var url2 = "http://www.youtube.com/watch?v="+id;
    $('#youtube_link_text').text(Traduz('watch_on_youtube'));
    $('#youtube_link').attr('nohref', url2);
    $('#youtube_param').attr('movie', url);
    $('#youtube_embed').attr('src', url);
    $('#youtube_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Twitcam(id)
{
    var url  = "http://static.livestream.com/chromelessPlayer/wrappers/TwitcamPlayer.swf?hash="+id;
    var url2 = "http://twitcam.com/"+id;
    $('#youtube_link_text').text('Twitcam');
    $('#youtube_link').attr('nohref', url2);
    $('#youtube_param').attr('movie', url);
    $('#youtube_embed').attr('src', url);
    $('#youtube_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Twitvid(id)
{
    var url  = "http://www.twitvid.com/player/"+id;
    var url2 = "http://www.twitvid.com/"+id;
    $('#youtube_link_text').text('Twitvid');
    $('#youtube_link').attr('nohref', url2);
    $('#youtube_param').attr('movie', url);
    $('#youtube_embed').attr('src', url);
    $('#youtube_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Vimeo(id)
{
    var url  = "http://vimeo.com/moogaloop_local.swf?ver=33457";
    var url2 = "http://vimeo.com/"+id;
    var vas  = "clip_id="+id+"&amp;server=vimeo.com&amp;autoplay=0&amp;fullscreen=1&amp;md5=0&amp;show_portrait=0&amp;show_title=0&amp;show_byline=0&amp;context=user:1445282&amp;context_id=&amp;force_embed=0&amp;multimoog=&amp;color=00ADEF&amp;force_info=true";
    $('#youtube_link_text').text('Vimeo');
    $('#youtube_link').attr('nohref', url2);
    $('#youtube_param').attr('movie', url);
    $('#youtube_var').attr('flashvars', vas);
    $('#youtube_embed').attr('src', url);
    $('#youtube_embed').attr('flashvars', vas);
    $('#youtube_preview').show();
    $('#user_info').hide();
    ShowPopup();
}

function Map(coo, polyg, tid, time)
{
    var list;
    if(time == 'list')
    {
        list = $('#tcon_list').attr('lista');
        var _tw = Controller().fec.ByTid(time, tid, null, list);
    }
    else
    {
        var _tw = Controller().fec.ByTid(time, tid);
    }

    window.GeoMap = {
        'location': coo,
        'polygon': polyg,
        'foto':  _tw.user.profile_image_url,
        'nome':  _tw.user.name,
        //texto: Controller().Utils.Regex.Replace(_tw.text),
        'texto': _tw.text,
        'loading': Traduz('loading')
    };
    if(!coo)
    {
        $('#map_link').hide();
    }else
    {
        $('#map_link').show();
        $('#map_link_text').text('Google Maps');
        $('#map_link').attr('nohref', 'http://maps.google.com/maps?q='+(coo.split(',').reverse().join(',')));
    }
    $('#map_preview_frame')[0].contentWindow.location.reload();
    $('#map_preview_frame').attr('src', 'mapa.html');
    $('#map_preview').show();
    $('#user_info').hide();



    ShowPopup();
}

function Traduzir(tid, tm)
{
    var list = null;
    if(tm == 'list')
    {
        list = $('#tcon_list').attr('lista');
    }

    var el = $('#twets_'+tm+' #texto_'+tid);
    var txt = unescape(el.attr('data'));
    var html = unescape(el.attr('html'));
    if(el.attr('traduzido'))
    {
        el.html(html);
        el.removeAttr('traduzido');
    }
    else
    {
        Controller().Utils.Translate.trans(txt, tid, tm, list);
    }
}

function TraduzirOK(txt, tid, tm)
{
    var el = $('#twets_'+tm+' #texto_'+tid);
    el.attr('traduzido', true);
    el.html(txt);
}

function TraduzirFAIL(re, tid, tm){
    Controller().console.log('TraduzirFAIL()');
    Controller().console.log(re);
}

var Utils = {

    Vendo:function()
    {
        var Timeout = 200;
        if(Controller().Chrowety.profile_last_view)
        {
          window.setTimeout('ShowUser(Controller().Chrowety.profile_last_view)', Timeout);
        }

        if(Controller().Utils.Twitpic.vendo)
        {
          window.setTimeout('Twitpic(Controller().Utils.Twitpic.vendo)', Timeout);
        }

        if(Controller().Utils.Twitgoo.vendo)
        {
          window.setTimeout('Twitgoo(Controller().Utils.Twitgoo.vendo)', Timeout);
        }

        if(Controller().Utils.Yfrog.vendo)
        {
          window.setTimeout('Yfrog(Controller().Utils.Yfrog.vendo)', Timeout);
        }
    },

    Size:{ // Utils.Size
        size:{},
        GetSizes:function(){ // Utils.Size.GetSizes()
            this.size.BodyHeight   = parseInt(document.height);
            this.size.BodyWidth    = parseInt($('#init').css('width'));
            this.size.HeaderHeight = parseInt($('#header').css('height'));
            this.size.TweetsHeight = parseInt($('.twets').css('height'));
        },

        Adjust:function(){ // Utils.Size.Adjust()
            Utils.Size.GetSizes();
            var sz = this.size,calc;
            var _config = Controller().config;
            console.log(sz);
            if(sz.BodyHeight && sz.HeaderHeight && sz.TweetsHeight){
                calc = (sz.BodyHeight - sz.HeaderHeight);
                sz.FinalHeight = calc;
                $('.twets').css('height', calc+'px');
                //$('.twets').html($('.twets').html()+' ');
                //$('#tconMaster').html($('#tconMaster').html());
                console.log(sz.TweetsHeight +' > '+ sz.FinalHeight);
            }

        }
    },

	UISets:function()
    {
	    var _config = Controller().config;
        $('.texwt').css({
            'font-size': _config.tweet_font_size,
            'text-shadow': _config.tweet_shadow,
        });
		$('.twets').css('height', _config.popup_height);
        //Utils.Size.Adjust();
        var height = parseInt(Controller().config.popup_height.split('px')[0]);

        if(isUp == true)
        {
            if($('#submenu_lists').text() != "")
            {
                //$('.twets').css('height', height-70+'px');
            }
            else
            {
                //$('.twets').css('height', height-50+'px');
            }

            //Utils.Size.Adjust()
        }

		if(_config.show_username =='true')
        {
            $('.tweets_names').show()
        }
	},

    ConversationsSets:function() // Utils.ConversationsSets()
    {
	    var _config = Controller().config;
        $('.conversations .texwt').css({
            'font-size': (parseInt(_config.tweet_font_size)-1)+'pt'
        });
        SearchData();
    }

};

function OAuthLogin()
{
    Controller().OAuth.Autorisar();
    return false;
}

function Login()
{
    var user  = $('#usuario').val();
    var senha = $('#senha').val();
    var lembra = $('#lembrar').val();

    var re = $('#status_login');

    if(!user || !senha)
    {
        re.fadeIn();
        if(!senha)
        {
            re.text(Traduz('password_empty'));
        }

        if(!user)
        {
            re.text(Traduz('user_empty'));
        }
    }
    else
    {
        re.fadeIn();
        Controller().Login(user, senha, lembra);
        re.text(Traduz('logging_in'));
    }

    return false;
}

function RequestToken()
{
    $('#autorize_button')[0].setAttribute("disabled","disabled");
    $('#autorize_button').html('<img id="autorize_image" src="img/spinner.gif" width="14" height="14" />');
    Controller().OAuth.DeletaToken('Popup().init()');
}

function Destacar()
{
    var PUP = 'popup.html';
    var PNM = 'Chrowety';
    var WCF = 'location=0,resizable=0,width=385,height=600';
    Controller().Utils.Popup.window = window.open(PUP,PNM,WCF);
}

function init()
{
    if(!Controller().Logado)
    {
      $('#OAuthLogin').show();
      if(Controller().OAuth.token.oauth_token)
      {
        $('#autorize_button').text(Traduz('sign_in_with_twitter'));
        $('#autorize_button').removeAttr('disabled');
      }
      else if(Controller().OAuth.token.erro)
      {
        $('#autorize_button').text('Twitter: '+Traduz('error'));
      }
      else
      {
        Controller().OAuth.RequestToken();
      }
      LoginUp = true;
      return false;
    }

    LoginUp = false;

    LoadProfile();
    RefreshLimit();
    $('#bempty').hide();
    $('#OAuthLogin').hide();
    $('#reply_text_update').val(Slogan);
    $('#init').show();

    document.title = '('+Controller().MostraCount()+') Chrowety ('+db.pega('user')+')';

    Utils.Vendo();
    LT.init();

    if(isFirstPopupOpen)
    {
        Controller().isFirstPopupOpen = true;
    }
}
</script>
<link rel="stylesheet" href="css/base.css" />
<link rel="stylesheet" href="css/tipsy.css" />
<script src="js/jquery-1.4.2.min.js"></script>
<script src="js/jquery.tipsy.js"></script>
<script src="js/prototypes.js"></script>
<script src="js/SpecialChars.js"></script>
</head>
<body onload="init()">
<div id="init" class="init dn" onkeydown="Atalho(event)">
  <div id="header">
      <div class="menu">
          <div id="load" class="load fr">
              <span id="load_result" class="fl"><script>html(Traduz('loading')+'...')</script></span>
              <img id="load_image"src="img/spinner.gif" class="fl" width="14" height="14"/>
          </div>
          <div class="info dn">
              <picture id="PhotoLink" class="pointer" onclick="Link(this, event);">
                <img id="foto_load" src="img/spinner.gif" class="photo fl" width="14" height="14" /><img id="foto" width="32" height="32" src="img/spinner.gif" class="photo fl dn"/>
              </picture>
              <span id="Profi_Data" class="fl dn">
                <span id="user_nome" class="p8"></span> <span class="cinza">(<span id="username" class="p8"></span>)</span> -
                <a class="p8" href="javascript:Controller().Logout();GoLogin();">
                  <script>html(Traduz('logout'))</script>
                </a>
                <br />
                <a id="SeguindoLink" class="cinza p8" onclick="Link(this, event);"><span id="seguindo"></span></a> | <a id="SeguidoresLink" class="cinza p8" onclick="Link(this, event);"><span id="seguidores"></span></a>
              </span>
          </div>

          <div>
              <button id="bhome" alerta="true" disabled="disabled" onclick="LT.home()" ondblclick="LT.home(true)"><script>html(Traduz('home'))</script></button>
              <button id="bmentions" alerta="true" disabled="disabled" onclick="LT.mentions()" ondblclick="LT.mentions(true)"><script>html(Traduz('mentions'))</script></button>
              <button id="bmessages" alerta="true" disabled="disabled" onclick="LT.messages()" ondblclick="LT.messages(true)"><script>html(Traduz('messages'))</script></button>
              <button id="bfavorites" alerta="true" disabled="disabled" onclick="LT.favorites()" ondblclick="LT.favorites(true)"><script>html(Traduz('favorites'))</script></button>
              <button id="blists" onclick="LT.Lists()"><script>html(Traduz('lists'))</script></button>
    <!--
              <button id="bmore" onclick="LT.more('more');"><script>html(Traduz('more'))</script></button>
    -->
              <img class="pointer fr" onclick="Link(this, event, true);" src="img/conf.png" width="16" height="16"/>
              <img class="pointer fr" onclick="Destacar()" src="img/window.png" width="16" height="16"/>
          </div>

      </div>

      <div id="submenu_lists" class="submenu centro"></div>

      <div id="submenu_more" class="submenu centro dn">
          <button id="botao_profile" onclick="QuickProfile();">Quick profile</button>
          <button id="botao_profile" onclick="AddPhoto();">Add Phtoo</button>
      </div>

      <div id="twets_update" class="update">
          <input type="hidden" id="update_type" value="update"/>
          <input type="hidden" id="dm_user" value=""/>
          <textarea id="reply_text_update" onclick="Update();" onkeydown="Atalho(event, 'update', 'home')" onkeyup="Count('update', 'update')" class="t1"></textarea>
          <div id="update_co2" class="text2 dn">
            <button class="button fl bt_char" style="margin-right:5px;" onclick="SpecialChars.show('update', 'update');">&hellip;</button>
            <div class="short fl" title="">
              <button onclick="ShortLink('update', 'update')"  class="button fl">
                  <img src="img/link.png" height="16" width="16"/>
              </button>
              <input onfocus="ShowShortLink('update', 'update')" class="short_link_url fl" id="short_link_url_update" type="text" />
            </div>
            <br/><br/>
            <button class="button_margin" id="botao_update" onclick="doUpdate()" title="Done (Enter)"><script>html(Traduz('done'))</script></button>
            <button class="button_margin" id="botao_share_update" onclick="SharePage('update', 'update')" title=""><script>html(Traduz('share_this_page'))</script></button>
            <a id="botao_update_cancel" onclick="UpdateCancel()" title="Cancel" class="p8"><script>html(Traduz('cancel'))</script></a>
            <span id="count_update" class="p13">140</span>
            <span id="reply_status_update" class="state"></span>
          </div>
      </div>
  </div>

  <div class="timeline"><!-- TIMELINE> -->
      <div id="tconMaster">

          <div id="tcon_home" class="twets_con dn">
            <div id="new_twets_home" class="new_twets dn" onclick="RefreshTweets('home')"></div>
            <div id="twets_home" class="twets">
                <div class="tload"><img src="img/spinner.gif" width="14" height="14" /></div>
            </div>
          </div>

          <div id="tcon_mentions" class="twets_con dn">
            <div id="new_twets_mentions" class="new_twets dn" onclick="RefreshTweets('mentions')"></div>
            <div id="twets_mentions" class="twets">
                <div class="tload"><img src="img/spinner.gif" width="14" height="14" /></div>
            </div>
          </div>

          <div id="tcon_messages" class="twets_con dn">
            <div id="new_twets_messages" class="new_twets dn" onclick="RefreshTweets('messages')"></div>
            <div id="twets_messages" class="twets">
                <div class="tload"><img src="img/spinner.gif" width="14" height="14" /></div>
            </div>
          </div>

          <div id="tcon_favorites" class="twets_con dn">
            <div id="new_twets_favorites" class="new_twets dn" onclick="RefreshTweets('favorites')"></div>
            <div id="twets_favorites" class="twets">
                <div class="tload"><img src="img/spinner.gif" width="14" height="14" /></div>
            </div>
          </div>

          <div id="tcon_list" class="twets_con dn">
            <div id="new_twets_list" class="new_twets dn" onclick="RefreshTweets(this.parentNode.getAttribute('lista'), true)"></div>
            <div id="twets_list" class="twets">
                <div class="tload"><img src="img/spinner.gif" width="14" height="14" /></div>
            </div>
          </div>

      </div>

      <div class="popup_bg dn" onclick="ShowUserERRO();"></div>

      <div class="popup dn">
        <span class="popup_close fr" onclick="ShowUserERRO();">X</span>

        <div id="quick_profile" class="dn centro">
            <div class="short fl" title="Quick Profile" style="padding-top:5px">
              <form onsubmit="return doQuickProfile();">
                <button onclick="doQuickProfile();" class="button fl" style="line-height:1.2em"><script>html(Traduz('username'))</script></button>
                <input class="fl" id="quick_profile_name" type="text" />
              </form>
            </div>
        </div>

        <div id="special_chars" class="dn centro">
            <span class="p12 oceania fl"><script>html(Traduz('special_chars'))</script></span>
            <br /><br />
            <div id="special_chars_container"></div>
        </div>

        <div id="map_preview" class="dn centro">
            <a id="map_link" class="cinza" onclick="Link(this, event);">
                <span id="map_link_text" class="p12 oceania fl"></span>
            </a>
            <br /><br />
            <iframe id="map_preview_frame" style="width:300px; height:350px; border:none;"></iframe>
        </div>

        <div style="background:transparent url('img/spinner.gif') no-repeat 130px 120px" id="youtube_preview" class="dn centro">
            <a id="youtube_link" class="cinza" onclick="Link(this, event);">
                <span id="youtube_link_text" class="p12 oceania fl"></span>
            </a>
            <br /><br />
            <object width="300" height="219" class="cb">
              <param id="youtube_param" name="movie" value="" />
              <param id="youtube_vars" name="flashvars" value="" />
              <param name="allowfullscreen" value="true" />
              <param name="allowscriptaccess" value="always" />
              <param name="wmode" value="transparent" />
              <embed id="youtube_embed" src="" wmode="transparent" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="219" />
            </object>
        </div>

        <div id="show_lists" loaded="false" class="dn centro" style="padding-top:5px;width:230px;">
            <span class="p12 oceania fl"><script>html(Traduz('add_list'))</script></span>
            <div class="short" style="padding-top:5px;width:260px;">
              <div class="opts">
                <a onclick="LT.Lists(true);"><script>html(Traduz('reload_lists'))</script></a>
              </div>
              <div id="show_lists_load">
                    <img id="show_lists_load_image" class="fl" src="img/spinner.gif" width="14" height="14" />
                    <span class="fl" style="margin-left:5px;">
                        <script>html(Traduz('loading'))</script>
                    </span>
                    <br />
              </div>

              <div id="show_lists_lists" class="dn"></div>
    <!--
              <br />
              <div class="opts">
                <button onclick="doQuickProfile();" class="button fl" style="line-height:1.2em"><script>html(Traduz('username'))</script></button>
                <input class="fl" id="quick_profile_name" type="text" />
              </div>
              <br /><br />
    -->
            </div>
        </div>
    <!--
        <div id="add_photo" class="dn centro">
            <div class="short fl" title="Quick Profile" style="padding-top:5px">
              <span class="p12 oceania fl">Add Photo</span>
              <br /><br />
              <form onsubmit="return doQuickProfile();">
                <button onclick="" class="button fl" style="line-height:1.2em">Select Photo</button>
                <input class="fl" id="quick_profile_name" type="text" />
                <br /><br />
                <button class="button fl" style="line-height:1.2em"><script>html(Traduz('username'))</script></button>
                <input class="fl" id="quick_profile_name" type="text" />
                <br /><br />
                <button class="button fl" style="line-height:1.2em"><script>html(Traduz('password'))</script></button>
                <input class="fl" id="quick_profile_name" type="text" />
                <br /><br /><br />
                <button onclick="doQuickProfile();" class="button fl" style="line-height:1.2em"><script>html(Traduz('done'))</script></button>
                <br />
                <button onclick="window.open('Upload.html','name','height=400,width=400');" class="button fl" style="line-height:1.2em">Popup</button>
              </form>
            </div>
        </div>
    -->
        <div id="twitpic_preview" class="dn centro">
            <br /><br />
            <a id="twitpic_link" onclick="Link(this, event);">
                <img id="twitpic_photo" class="photo" width="250"/>
            </a>
        </div>

        <div id="user_info" class="dn">
            <div style="height:100px">
              <img id="user_photo" onclick="PictureZoom(this);" width="100" height="100" class="photo fl" zoom="in"/>
              <span id="user_data" class="fl">
                   <span id="user_protegido" class="cadeado dn" title="Protected">Protegido</span>
                   <span id="user_unome" class="p12 oceania"></span><br />
                   <span><a id="user_uname" href="#" onclick="Link(this, event);"></a></span><br />
                   <a class="cinza" href="#" onclick="Link(this, event);" id="user_local_link"><span id="user_local"></span></a><br />
                   <span><script>html(Traduz('member_since'))</script>: <span id="user_since"></span></span><br />
                   <span id="user_follow" class="dn">
                     <script>html(Traduz('following_you',':'))</script>
                     <span id="following_re"></span>
                   </span><br />
                   <img id="user_verified" src="img/verified.png" class="dn" width="16" height="16"/>
              </span>
            </div>
            <div id="user_rest">
    	        <br />
    	        <div id="user_bio_div" class="cb">
    	            <span><a id="user_link" onclick="Link(this, event)"></a></span><br />
    	            <span id="user_bio"></span>
    	            <br /><br />
    	        </div>
    	        <div class="centro" style="border-top:1px solid #ddd;border-bottom:1px solid #ddd;">
    	            <span class="centro inblock" style="width:70px">
    	              <span id="user_followers"></span>
    	              <br />
    	              <span><script>html(Traduz('followers'))</script></span>
    	            </span>

    	            <span class="centro inblock" style="width:70px">
    	              <span id="user_following"></span>
    	              <br />
    	              <span><script>html(Traduz('following'))</script></span>
    	            </span>

    	            <span class="centro inblock" style="width:70px">
    	              <span id="user_tweets"></span>
    	              <br />
    	              <span><script>html(Traduz('tweets'))</script></span>
    	            </span>

    	            <span class="centro inblock dn" style="width:70px">
    	              <span id="user_lists"></span>
    	              <br />
    	              <span><script>html(Traduz('lists'))</script></span>
    	            </span>
    	        </div>
    	        <div id="user_update_div" class="dn">
    	            <br /><br />
    	            <b><script>html(Traduz('last_update'))</script>: </b>
    	            <span id="user_update"></span><br />
    	            <span class="p8 cinza">
                        <a class="p8 cinza" id="user_update_data" onclick="Link(this, event);"></a>
                        <span id="user_update_via"></span>
                    </span>
    	        </div>
    			<br>
                <div id="user_options_div" class="user_profile_options dn">
                    <br />
                    <span id="user_options_follow" title="Follow" class="user_opt r_all"><span><script>html(Traduz('follow'))</script></span></span>
                    <span id="user_options_unfollow" title="Unfollow" class="user_opt r_all"><script>html(Traduz('unfollow'))</script></span>
                    <span id="user_options_block" title="Block" class="user_opt r_all"><span class="block">Block</span></span>
                    <span id="user_options_spam" title="Spam" class="user_opt r_all"><span class="spam">Spam</span></span>
                    <span id="user_options_lists" title="Lists" class="user_opt r_all list"><span class="list">Lists</span></span>
                    <br /><br />
                    <span id="user_options_status" class="dn"></span>
                </div>
            </div>
        </div>
      </div>
  </div><!-- <TIMELINE -->
</div>

<div id="OAuthLogin" class="login dn">
    <form onsubmit="return OAuthLogin();">
        <br />
        <button disabled="disabled" id="autorize_button" type="submit"><img id="autorize_image" src="img/spinner.gif" width="14" height="14" /></button>
        <br /><br />
        <a onclick="RequestToken();" class="p8">(<script>html(Traduz('request_new_auth'))</script>)</a>
        <br />
        <br />
    </form>
    <script>html(Traduz('or'))</script>
    <br /><br />
    <div class="bar pointer" onclick="$('#api_login').slideToggle(function(){$('#usuario').focus();});">API Login</div>
    <form id="api_login"class="dn" onsubmit="return Login();">
        <br />
        <label class="inp"><script>html(Traduz('username'))</script>:
            <input id="usuario" type="text" size="10" tabindex="1"/>
        </label>
        <br /><br />
        <label class="inp"><script>html(Traduz('password'))</script>:
            <input id="senha" type="password" size="10" tabindex="2" />
        </label>
        <br /><br />
        <label class="check">
            <input id="lembrar" type="checkbox" tabindex="3"/>
            <script>html(Traduz('remember'))</script>
        </label>
        <br /><br />
        <button type="submit" tabindex="3"><script>html(Traduz('login'))</script></button>
        <br /><br />
        <span id="status_login" class="dn"></span>
    </form>
</div>
</body>
</html>